//
//  ViewController.m
//  仿IPad的pop功能
//
//  Created by lili on 16/8/24.
//  Copyright © 2016年 陈浩. All rights reserved.
//

#import "ViewController.h"
#import "CHPopverController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) CHPopverController *popVc;
@property (nonatomic ,strong) NSArray *titles;
@property (nonatomic ,strong) NSArray *images;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    self.title = @"仿iPad的Pop功能";
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:@"菜单" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    btn.frame = CGRectMake(0, 0, 40, 40);
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
}

-(void)rightBtnClick:(UIButton *)btn{
    
    if (!self.popVc) {
        //  显示popVc
        self.titles = @[@"撤回",@"确认",@"记录"];
        self.images = @[@"icon_button_recall",@"icon_button_record",@"icon_button_affirm"];
       UITableViewController *tvContr = [[UITableViewController alloc] init];
        tvContr.tableView.backgroundColor = [UIColor clearColor];
        tvContr.tableView.delegate = self;
        tvContr.tableView.dataSource = self;
        tvContr.tableView.bounces = NO;
        tvContr.tableView.scrollEnabled = NO;
        tvContr.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        CHPopverController *popVc = [[CHPopverController alloc] initWithContentViewController:tvContr];
        popVc.popoverContentSize = CGSizeMake(100, self.titles.count *43);
        self.popVc = popVc;
    }
    [self.popVc presentPopoverInView:btn arrowDirection:CZPopverArrowDirectionRight];
    
}



#pragma mark - UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titles.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    cell.textLabel.text = self.titles[indexPath.row];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.imageView.image = [UIImage imageNamed:self.images[indexPath.row]];
    cell.backgroundColor = [UIColor clearColor];
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, cell.bounds.size.height - 1, cell.bounds.size.width, 1)];
    lineView.backgroundColor = [UIColor lightGrayColor];
    [cell addSubview:lineView];
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    NSLog(@" 点击了%@",self.titles[indexPath.row]);
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
